#!/usr/bin/php
<?php

ini_set('display_errors','On');
error_reporting(1);

Class Advert_Import
{
var $production;
var $development;

function Advert_Import()
{
## PREPARE CONNECTION ##
/*
$this->production=mysql_connect("localhost","infopark_com_ua","8888zG6Wtv1k",true);
mysql_set_charset ('utf8',$this->production);
mysql_select_db("infopark_com_ua",$this->production);
*/
$this->development=mysql_connect("localhost","infopark_com_ua","8888zG6Wtv1k",true);
mysql_set_charset ('utf8',$this->development);
mysql_select_db("dev_infopark_com_ua1",$this->development);

$this->update_cities(20);

mysql_close($this->development);
#mysql_close($this->production);
}
function update_cities($tid)
{

$sql="SELECT * FROM node WHERE type='advert'";

$i=0;
$nodes=$this->LIST_Q($sql,$this->development);

foreach($nodes as $node)
  {
  
  
  $row=$this->ROW_Q("SELECT * FROM taxonomy_index WHERE tid=".$tid." AND nid=".$node->nid,$this->development);
  #pre($row);
  #die();
  if(!$row)
	{
	$i++;
	$sql1="INSERT INTO taxonomy_index(nid,tid,sticky,created) VALUES (".$node->nid.",".$tid.",0,".time().")";
	$this->QUERY($sql1,$this->development);
	print $sql1."\n";
	}
  }
  print "Updated ".$i." categories\n\n";
}

function QUERY($sql,$conn)
{
#$$result=mysql_query($sql,$conn);
$result=mysql_query($sql,$conn) or die($sql." Error \n\n".mysql_error($conn));
return $result;
}
function ROW_Q($sql,$conn)
{
$result=$this->QUERY($sql,$conn);
$r=mysql_fetch_object($result);
if(count($r)==0)$r=false;
return $r;
}
function LIST_Q($sql,$conn)
{

$result=$this->QUERY($sql,$conn);
while($r=mysql_fetch_object($result))
  {
  $data[]=$r;
  }
  
  if(count($data)==0)$data=false;
  return $data;
}

}
function pre($array)
{
print_r($array);
}

$AI=new Advert_Import;


print "### GOOD FINALL ###\n\n";
?>